﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using MLM.Data;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class BonusDetailByMonthRecursive : BasePage
    {
        public static UserTree userTree = new UserTree();
       public static List<UserTree> users = new List<UserTree>();
        protected void Page_Load(object sender, EventArgs e)
        {
            int thisYear = Convert.ToInt32(System.DateTime.Now.Year);
            for (int i = 2013; i <= thisYear; i++)
            {
                FromYearCbb.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i < 13; i++)
            {
                MonthCbb.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            if (!Permission.IsAdmin(Permission.GetCurrentUserId()))
            {
                UserIDPanel.Attributes.Add("style", "display:none");
                UserIDTxt.Text = Permission.GetCurrentUserId() + "";
            }
            else
            {
                UserIDPanel.Attributes.Add("style", "display:inline-block");
            }
            
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetTree(string _userId, string _level)
        {
            users.Clear();
            long userId = long.Parse(_userId);
            int level = int.Parse(_level);
            MLMDataContext ct = new MLMDataContext();
            UserTree treeUser = new UserTree();
            treeUser = BuildTree(ct, userId, 1, level);
            ResponseData data = new ResponseData();
            data.MaxLevels = Maxlevels;
            data.Data = users;
            JavaScriptSerializer ser = new JavaScriptSerializer();
            return ser.Serialize(data);
        }

        public static int Maxlevels = 1;
        public class ResponseData
        {
            public int MaxLevels { get; set; }
            public object Data { get; set; }
        }
        public static UserTree BuildTree(MLMDataContext ct, long userId, int currentLevel, int maxLevel)
        {
            UserTree tree = new UserTree();
            if (currentLevel <= maxLevel)
            {
                var user = ct.Users.FirstOrDefault(it => it.UserId == userId);
                tree.Name = "";
                tree.Name = user.FirstName + " " + user.LastName+"";
                tree.UserId = userId;
                tree.Level = currentLevel;
                tree.SponsorId = long.Parse((user.SponsorID ?? 0).ToString());
                users.Add(tree);
                var sponsorList = ct.Users.Where(it => it.SponsorID == userId);
                if (sponsorList.Count() > 0)
                {
                    currentLevel++;
                    Maxlevels = currentLevel;
                    tree.RecommendUsers = new List<UserTree>();
                    foreach (var sponsor in sponsorList)
                    {
                        //tree.RecommendUsers.Add(BuildTree(ct, sponsor.UserId, currentLevel, maxLevel));
                        BuildTree(ct, sponsor.UserId, currentLevel, maxLevel);
                    }
                }
            }
            return tree;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetRanks()
        {
            MLMDataContext ct = new MLMDataContext();
            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<Ranks> ranks = new List<Ranks>();
            foreach (var rank in ct.Ranks.OrderByDescending(it => it.Bonus))
            {
                ranks.Add(new Ranks
                               {
                                   Bonus = rank.Bonus,
                                   MinPV = rank.MinPV,
                                   Name = rank.Name
                               });
            }
            var str = ser.Serialize(ranks);
            str = str.Replace("\"", "\\\"");
            return str;
        }
    }
    public class UserTree
    {
        public string Name { get; set; }
        public long UserId { get; set; }
        public long SponsorId { get; set; }
        public int Level { get; set; }
        public List<UserTree> RecommendUsers { get; set; }
    }
    public class Ranks
    {
        public string Name { get; set; }
        public double? MinPV { get; set; }
        public double? Bonus { get; set; }
    }
}